﻿using System;
using System.Diagnostics;
using Microsoft.SharePoint;

namespace Ordina.SharePoint.CollectAndCrop
{
    /// <summary>
    /// Writes trace output using the System.Diagnostics.Trace class. Trace will be directed to all of 
    /// the configured trace listeners.
    /// 
    /// voeg toe aan web.config:
    /// <system.diagnostics>
    ///	    <switches>
    ///        <!-- 0=none, 1=errors, 2=warnings, 3=info, 4=verbose -->
    ///        <add name="Ordina.SharePoint.CollectAndCrop" value="4" />
    ///		</switches>
    ///		<trace autoflush="true" indentsize="4">
    ///			<listeners>
    ///				<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" 
    ///					initializeData="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS\trace.log" />
    ///				<!--<remove name="Default" />  -->
    ///			</listeners>
    ///		</trace> 
    /// </system.diagnostics>
    /// 
    /// </summary>
    /// <created date="07 October 2008" by="Chris O'Brien">Initial version</created>
    public class TraceHelper
    {
        private string ownerClassName = string.Empty;

        /// <summary>
        /// The constructor
        /// </summary>
        /// <param name="OwnerClass">the class</param>
        public TraceHelper(object OwnerClass)
        {
            ownerClassName = OwnerClass.GetType().FullName;
        }

        /// <summary>
        /// The constructor
        /// </summary>
        /// <param name="OwnerClassName">the classname</param>
        public TraceHelper(string OwnerClassName)
        {
            ownerClassName = OwnerClassName;
        }

        /// <summary>
        /// Write trace output using System.Diagnostics.Trace. Watchout with { and } in the Message (e.q. guids) will throw exceptions
        /// </summary>
        /// <param name="Write">Condition to test before writing - pass trace switch severity.</param>
        /// <param name="Level">Severity level.</param>
        /// <param name="Message">Message to write.</param>
        /// <param name="Args">Arguments to insert into message.</param>
        public void WriteLineIf(bool Write, TraceLevel Level, string Message, params object[] Args)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    Trace.WriteLineIf(Write, string.Format("{0} {1} [{2}] - {3} : {4}",
                                                           DateTime.Now.ToShortDateString(),
                                                           DateTime.Now.ToLongTimeString(),
                                                           Level.ToString().ToUpper(),
                                                           ownerClassName,
                                                           string.Format(Message, Args)));
                }
                catch (Exception ex)
                {
                    Trace.WriteLineIf(Write, string.Format("Error writing to tracefile: " + ex.ToString()));
                }
            });
        }
    }
}
